document.addEventListener("DOMContentLoaded", function () {
  chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
    document.getElementById("elementHtml").innerText = JSON.stringify(tabs[0]);
    chrome.tabs.sendMessage(
      tabs[0].id,
      { action: "show", text: "hello from popup" },
      function (response) {
        console.log("from popup resp script:", response);
        document.getElementById("elementHtml").innerHTML =
          // JSON.stringify(response);
          response;
      }
    );
  });

  let context;
  try {
    context = browser;
  } catch (error) {
    context = chrome;
  }
  context.runtime.onMessage.addListener((message) => {
    if (message.action === "show") {
      document.getElementById("elementHtml").innerHTML = 12312414;
      console.log("from popup:", message);
    }
  });
});

function copyToClipboard(element) {
  console.log("copyToClipboard");
  try {
    let textarea = document.getElementById("elementHtml");
    textarea.select();
    console.log("copy", textarea.value);
    document.execCommand("copy");
    textarea.unselect();

    let btn = document.getElementById("copyButton");
    btn.innerHTML = "Copied!";
    setTimeout(() => {
      btn.innerHTML = "Copy";
    }, 2000);
  } catch (error) {
    console.log("copyToClipboard error", error);
  }
}
document
  .getElementById("copyButton")
  .addEventListener("click", copyToClipboard);
